package com.ithinkstudio.jeez.web;

import com.ithinkstudio.jeez.domain.Result;
import com.ithinkstudio.jeez.entity.User;
import com.ithinkstudio.jeez.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @description: 用户CURD页面
 * @author: fengzp
 * @create: 2020/04/14
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    /**
     * 根据用户ID查询用户信息
     * @param id
     * @return
     */
    @GetMapping("find/{id}")
    public String findById(@PathVariable("id") Integer id, ModelMap modelMap) {
        User model = userService.getById(id);
        modelMap.addAttribute("model", model);
        return "user";
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    @ResponseBody
    public Result<String> delete(@PathVariable("id") Integer id) {
        userService.removeById(id);
        return Result.getSuccess();
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public Result<String> add(@RequestBody User user) {
        userService.save(user);
        return Result.getSuccess();
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public Result<String> update(@RequestBody User user) {
        userService.updateById(user);
        return Result.getSuccess();
    }

}
